﻿/// <reference name="MicrosoftAjax.js"/>
/// <reference name="Doo.WebUI.core.js"/>
/// <reference name="Doo.WebUI.DataSource.base.script.js"/>

Type.registerNamespace("Doo.DataSource");

Doo.DataSource.ObjDataSource = function (element) {
    Doo.DataSource.ObjDataSource.initializeBase(this, [element]);
}

Doo.DataSource.ObjDataSource.prototype = {
    initialize: function () {
        if (!this.selectargs) this.selectargs = [];
        Doo.DataSource.ObjDataSource.callBaseMethod(this, 'initialize');
        // Add custom initialization here
    },
    dispose: function () {
        //Add custom dispose actions here
        Doo.DataSource.ObjDataSource.callBaseMethod(this, 'dispose');
    },
    alias: false,
    set_alias: function (value) { this.alias = value; window[this.alias] = this; },
    get_alias: function () { return this.alias; },
    objname: false,
    set_objname: function (value) { this.objname = value; },
    get_objname: function () { return this.objname; },
    selectmethod: false, //获取数据的方法
    set_selectmethod: function (value) { this.selectmethod = value; },
    get_selectmethod: function () { return this.selectmethod; },
    selectargs: null,
    set_selectargs: function (value) { this.selectargs = value; },
    get_selectargs: function () { return this.selectargs; },
    versionmethod: false, //检查版本的方法，该方法返回一个参数用于附加在selectmethod的参数上。
    set_versionmethod: function (value) { this.versionmethod = value; },
    get_versionmethod: function () { return this.versionmethod; },
    currentversion: null, //当前版本
    lastcheckversiontime: null, //上次检查版本时间

    /* 参数初始化 */
    set_initselectargs: function (args) {
        //传入数组
        this.selectargs = new Array();
        for (var i = 0; i < args.length; i++) {
            var arg = args[i];
            var item = null;
            switch (arg.type) {
                case "c": //cookie
                    item = new Doo.ParameterCookie();
                    item.cookieName = arg.cookieName;
                    break;
                case "q": //query
                    item = new Doo.ParameterQueryString();
                    item.queryName = arg.queryName;
                    break;
                case "e": //element
                    item = new Doo.ParameterControl();
                    item.controlid = arg.controlid;
                    item.propname = arg.propertyName;
                    break;
                case "p": //parameter
                default:
                    item = new Doo.Parameter();
                    break;
            }
            item.name = arg.name;
            item.defaultValue = arg.defaultValue;
            this.selectargs.push(item);
        }
    },
    get_initselectargs: function () { return null; }, //永远返回null

    appendSelectArg: function (name, defaultValue, typeName, custom1, custom2) {
        var args = this.get_selectargs();
        if (name in args) {
            throw new Error("已经存在的参数：" + name);
        }
        var type = Doo["Parameter" + (typeName || "")];
        if (!type) throw new Error("错误的类型：" + typeName);
        var item = new type();
        item.name = name;
        item.defaultValue = defaultValue;
        switch (typeName) {
            case "Cookie":
                item.cookieName = custom1;
                break;
            case "QueryString":
                item.queryName = custom1;
                break;
            case "Control":
                item.controlid = custom1;
                item.propname = custom2;
                break;
        }
        args.push(item);
    },

    /* 中途修改参数 */
    set_selectarg: function (name, value) {
        var args = this.get_selectargs();
        for (var i = 0; i < args.length; i++) {
            if (args[i].name == name) {
                args[i].defaultValue = value;
                return;
            }
        };
        throw new Error("不存在的参数");
    },

    /* select方法 */
    select: function (onsucc, onfail, option, userContext) {
        ///<summary>从web服务读取</summary>

        Doo.DataSource.ObjDataSource.callBaseMethod(this, 'select');

        if (option == null) option = null;
        var args = option ? option.args ? option.args : {} : {};
        var scope = this.get_scripts();
        for (var i = 0; i < this.selectargs.length; i++) {
            var arg = this.selectargs[i];
            if (arg.name in args) continue;
            args[arg.name] = arg.get_value(scope);
        }

        var selectmethod = eval(this.objname + "." + this.selectmethod);
        if (this.enableversion) { args.version = this.get_version(); };
        selectmethod(args, this.selectsucc, this.selectfail, { onsucc: onsucc, onfail: onfail, option: option, userContext: userContext, ods: this });
    },

    /* insert,update */
    insertmethod: '',

    insert: function (obj, option) {
        //插入

    },

    updatemethod: '',
    update: function (obj, option) {
        ///<summary>从web服务更新，updatemethod传递的参数有updateargs,updateobj</summary>
        var updatemethod = eval(this.objname + "." + this.selectmethod);
    },

    empty: null

}

Doo.DataSource.ObjDataSource.registerClass('Doo.DataSource.ObjDataSource', Doo.DataSource.Base);

Doo.DataSource.ObjDataSource.prototype.Select = Doo.DataSource.ObjDataSource.prototype.select;
